package net.cyndeline.GUI.setup

import net.cyndeline.GUI.graphics.{Representation, EntityRepresentation, Animation, SpriteSheet}
import net.cyndeline.misc.Bag

/**
 * Reads all sprite sheets and separates them manually into animations. The animations are then stored into
 * representation containers with a key indicating what they represent (wall, knight etc.). Representations are
 * produced using static calls to this object.
 */
object SpriteReader {
  private val representationFactories = new Bag[() => Representation]()

  /**
   * Public method that produces representations of the given type.
   */
  def makeRepresentation(t: RepresentationType): Representation = representationFactories.get(t.id).getOrElse {
    throw new Error("No representation factory set for type " + t.getClass)
  }.apply()

  /*
   * Representation definitions.
   */

  /*
   * Dungeon tiles
   */
  private val dungeonSheet = new SpriteSheet("/img/tilesets/dungeon/temp_dungeon.png", 32, 32)
  private val wallImage = dungeonSheet.readSprite(0, 3)
  private val floorImage = dungeonSheet.readSprite(12, 3) // Not really a floor gfx, but whatever

  private val wallAnimation = new Animation(10, wallImage)
  private val floorAnimation = new Animation(10, floorImage)

  private def makeDungeonWall(): Representation = {
    val dungeonWallRep = new EntityRepresentation(wallAnimation)
    dungeonWallRep.addAnimation(wallAnimation, Idle)
    dungeonWallRep
  }
  setFactory(Dungeon_wall, makeDungeonWall)

  private def makeDungeonFloor(): Representation = {
    val dungeonFloorRep = new EntityRepresentation(floorAnimation)
    dungeonFloorRep.addAnimation(floorAnimation, Idle)
    dungeonFloorRep
  }
  setFactory(Dungeon_floor, makeDungeonFloor)

  private def setFactory(t: RepresentationType, f: () => Representation) {
    representationFactories.set(t.id, f)
  }
}
